/*
 * This file is part of aion-engine <aion-engine.com>
 *
 * aion-engine is private software: you can redistribute it and or modify
 * it under the terms of the GNU Lesser Public License as published by
 * the Private Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aion-engine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License
 * along with aion-engine.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.controllers.observer;

import com.aionengine.gameserver.model.gameobjects.Creature;
import com.aionengine.gameserver.model.gameobjects.player.Player;
import com.aionengine.gameserver.model.shield.Shield;
import com.aionengine.gameserver.model.utils3d.Point3D;
import com.aionengine.gameserver.services.SiegeService;
import com.aionengine.gameserver.utils.MathUtil;
import com.aionengine.gameserver.utils.PacketSendUtility;

/**
 * @author Wakizashi, Source
 */
public class ShieldObserver extends ActionObserver {

    private Creature creature;
    private Shield shield;
    private Point3D oldPosition;

    public ShieldObserver() {
        super(ObserverType.MOVE);
        this.creature = null;
        this.shield = null;
        this.oldPosition = null;
    }

    public ShieldObserver(Shield shield, Creature creature) {
        super(ObserverType.MOVE);
        this.creature = creature;
        this.shield = shield;
        this.oldPosition = new Point3D(creature.getX(), creature.getY(), creature.getZ());
    }

    @Override
    public void moved() {
        boolean passedThrough = false;
        boolean isGM = false;

        if (SiegeService.getInstance().getFortress(shield.getId()).isUnderShield())
            if (!(creature.getZ() < shield.getZ() && oldPosition.getZ() < shield.getZ()))
                if (MathUtil.isInSphere(shield, (float) oldPosition.getX(), (float) oldPosition.getY(),
                        (float) oldPosition.getZ(), shield.getTemplate().getRadius()) != MathUtil.isIn3dRange(shield, creature,
                        shield.getTemplate().getRadius()))
                    passedThrough = true;

        if (passedThrough) {
            if (creature instanceof Player) {
                PacketSendUtility.sendMessage(((Player) creature), "You passed through shield.");
                isGM = ((Player) creature).isGM();
            }

            if (!isGM) {
                if (!(creature.getLifeStats().isAlreadyDead()))
                    creature.getController().die();
                if (creature instanceof Player)
                    ((Player) creature).getFlyController().endFly(true);
                creature.getObserveController().removeObserver(this);
            }
        }

        oldPosition.x = creature.getX();
        oldPosition.y = creature.getY();
        oldPosition.z = creature.getZ();
    }

}